package com.jing.weibo.controller.timeline;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import weibo4j.model.WeiboException;

import com.jing.system.comm.BaseController;
import com.jing.system.comm.contants.SystemCons;
import com.jing.system.comm.contants.WeiboCons;
import com.jing.weibo.pojo.Fansattention;
import com.jing.weibo.pojo.UserInfo;
import com.jing.weibo.ws.sina.SinaService;

/**
 * 发布微博的Controller
 * @author jing.yue
 * @version 2012/09/11 1.0.0
 */
@Controller
public class IssueWeiboController extends BaseController {

	private static final Logger logger = Logger.getLogger(IssueWeiboController.class);

	@Autowired
	private SinaService sinaService;

	/**
	 * 用户分享妞妞助手到微博
	 * @return
	 */
	@RequestMapping(value = "/timeline/json/shareWeibo")
	@ResponseBody
	public ModelMap shareWeibo(HttpServletRequest request, Fansattention fansattention) {
		// TODO Auto-generated method stub
		//logger.info("用户分享妞妞助手到微博");
		String result = null;
		StringBuffer errorWeiboMsg = new StringBuffer();
		UserInfo userInfo = getSessionUser(request);
		try {
			sinaService.updateStatus(SystemCons.getUserShareWeiboContent(), userInfo.getToken());
			result = SUCCESS;
		} catch (WeiboException e) {
			logger.error(WeiboCons.getErrorMsg(e));
			result = ERROR_WEIBO;
			errorWeiboMsg.append("调用微博接口出现异常[").append(WeiboCons.getErrorMsg(e)).append("]!");
		} catch (Exception e) {
			result = ERROR;
			logger.error(userInfo.getNickname() + ": 取消单向关注信息异常!", e);
		}
		//logger.info(errorWeiboMsg.toString());
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		modelMap.put("errorWeiboMsg", errorWeiboMsg.toString());
		return modelMap;
	}

}